<?php

namespace App\Controller\Home;


use App\Controller\Example\NoticeExample;
use App\Models\BannerModel;
use App\Models\BarsClassModel;
use App\Models\Conn;
use App\Models\NoticeModel;
use Core\Common\Common;
use Core\Result\Result;

class Home
{
    /**
     * 客户端获取轮播图
     * entry
     * return json
     * */
    public function banner($data){
        Result::success(
            BannerModel::getBannerList(new Conn(),$data["position"])
        );
    }
    /**
     * 分类列表
     * entry
     * return json
     * */
    public function typeList(){
        Result::success(BarsClassModel::barsClassList(new Conn()));
    }
    /**
     * 首页轮播公告
     * entry
     * return json
     * */
    public function homeNotice()
    {
        Result::success(NoticeModel::getNewNotice(new Conn()));
    }
    /**
     * 公告列表
     * entry page limit
     * return json
     * */
    public function noticeList($data)
    {
        $assemble = Common::identical($data);
        Result::success(NoticeModel::getNoticeList(new Conn(),$assemble));
    }
    /**
     * 公告详情
     * entry id
     * return json
     * */
    public function noticeDetails($data)
    {
        $assemble = Common::assemble(new NoticeExample(), $data);
        Result::success(NoticeModel::getNoticeDetails(new Conn(),$assemble));
    }
    /**
     * 礼包商品
     * entry page limit
     * return json
     * */
    public function packageGoods($data){
        $name = $data["name"];
        $cid = $data["cid"];
        $where = '';
        if($name){
            $where .= " and `name` like '%$name%'";
        }
        if($cid){
            $where .= " and `cid` = $cid";
        }
        $identical = Common::identical($data);
        $goodsSql="SELECT `id`, `name`,`synopsis`, `price`, `original_price`, `cost_price`, (`sales`+`reality_sales`) as sales, `img`, `video`, `time`, `stock`, `status`, `type`, `sku` FROM `bars_goods` WHERE `type` = 1 and `status` =1 $where order by sort desc,`time` desc limit $identical->pageStart,$identical->limit";
        $conn = new Conn();
        $readConn = $conn->readConn($goodsSql, true);
        foreach ($readConn as $k=>$v){
            if($v['sku']){
                $readConn[$k]['sku'] = json_decode($v['sku'],true);
            }
        }
        $goodsSqlCount="SELECT count(`id`) as count FROM `bars_goods` WHERE `type` = 1 and `status` =1 $where";
        $ret["list"] = $readConn;
        $ret["count"] = $conn->readConn($goodsSqlCount)["count"];
        Result::success($ret);
    }
    /**
     * 普通商品
     * @param $data
     */
    public function consumeGoodsList($data){
        $identical = Common::identical($data);
        $content = $data["name"];
        $cid = $data["cid"];
        $where = '';
        if($content){
            $where .= " and `name` like '%$content%'";
        }
        if($cid){
            $where .= " and `cid` = $cid";
        }
        $goodsSql="SELECT `id`, `name`,`synopsis`, `price`, `original_price`, `cost_price`, (`sales`+`reality_sales`) as sales, `img`, `video`, `time`, `stock`, `status`, `type`, `sku` FROM `bars_goods` WHERE `status` =1 $where order by sort desc,`time` desc limit $identical->pageStart,$identical->limit";
        $conn = new Conn();
        $readConn = $conn->readConn($goodsSql, true);
        /*if($readConn){
            foreach ($readConn as $k=>$v){
                /*$integral = number_format($v['price'] * $config['val'],2,'.','');
                $readConn[$k]['price'] = number_format($v['price'] - $integral,2,'.','');;
                $readConn[$k]['integral'] = $integral;
                $readConn[$k]['price'] = number_format($v['price'] - $v['integral'],2,'.','');
                if($v['sku']){
                    $readConn[$k]['sku'] = json_decode($v['sku'],true);
                    foreach ($readConn[$k]['sku'][0]['arr'] as $kk=>$vv){
                        $readConn[$k]['sku'][0]['arr'][$kk]['price'] = number_format($vv['price'] - $v['integral'],2,'.','');
                    }
                }
            }
        }*/
        $resData["list"] = $readConn;
        $goodsCountSql="SELECT count(`id`) as counts FROM `bars_goods` WHERE `type` = 2 and `status` =1 $where";
        $goodsCountResult = $conn->readConn($goodsCountSql);
        $resData["count"] = $goodsCountResult["counts"];
        Result::success($resData);
    }
    /**
     * 商品详情
     * entry  gid
     * return json
     * */
    public function goodsDetails($data){
        $gid = $data["gid"];//商品id
        $conn = new Conn();
        $goodsSql="SELECT `id`, `bid`, `cid`, `name`, `synopsis`, `price`,`integral`,`original_price`, `cost_price`, (`sales`+`reality_sales`) as sales, `img`, `video`, `details`, `time`, `stock`, `status`, `type`, `discount`,`views`,`rebate`,`sku`,`times` FROM `bars_goods` WHERE `id` = $gid and `status` =1";
        $readConn = $conn->readConn($goodsSql);
        if(empty($readConn)){
            Result::error("您访问的商品不存在");
        }
        $goodsImgSql="SELECT `img` FROM `goods_img` WHERE `gid` = $gid";
        $goodsImgList = $conn->readConn($goodsImgSql, true);
        $readConn["imgList"] = $goodsImgList;
        $upSql="UPDATE `bars_goods` SET `views`=`views`+1,`sales`=`sales`+1 WHERE `id` = $gid";
        $conn->executeConn($upSql);
        Result::success($readConn);
    }

}